package com.zenithmind.exam.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zenithmind.exam.pojo.entity.Question;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 题目Mapper接口
 * 
 * @author ZenithMind
 * @since 2024-07-01
 */
@Mapper
public interface QuestionMapper extends BaseMapper<Question> {

    /**
     * 根据题库ID查询题目列表
     * 
     * @param bankId 题库ID
     * @return 题目列表
     */
    @Select("SELECT * FROM exam_question WHERE bank_id = #{bankId} AND is_deleted = 0 ORDER BY create_time DESC")
    List<Question> selectByBankId(@Param("bankId") String bankId);

    /**
     * 根据题目类型查询题目列表
     * 
     * @param type 题目类型
     * @return 题目列表
     */
    @Select("SELECT * FROM exam_question WHERE type = #{type} AND status = 1 AND is_deleted = 0 ORDER BY create_time DESC")
    List<Question> selectByType(@Param("type") Integer type);

    /**
     * 根据难度查询题目列表
     * 
     * @param difficulty 难度等级
     * @return 题目列表
     */
    @Select("SELECT * FROM exam_question WHERE difficulty = #{difficulty} AND status = 1 AND is_deleted = 0 ORDER BY create_time DESC")
    List<Question> selectByDifficulty(@Param("difficulty") Integer difficulty);

    /**
     * 分页查询题目列表
     * 
     * @param page 分页参数
     * @param bankId 题库ID
     * @param title 题目标题（模糊查询）
     * @param type 题目类型
     * @param difficulty 难度等级
     * @param status 状态
     * @return 分页结果
     */
    IPage<Question> selectQuestionPage(Page<Question> page,
                                      @Param("bankId") String bankId,
                                      @Param("title") String title,
                                      @Param("type") Integer type,
                                      @Param("difficulty") Integer difficulty,
                                      @Param("status") Integer status);

    /**
     * 随机查询题目列表
     * 
     * @param bankId 题库ID
     * @param type 题目类型
     * @param difficulty 难度等级
     * @param limit 限制数量
     * @return 题目列表
     */
    List<Question> selectRandomQuestions(@Param("bankId") String bankId,
                                        @Param("type") Integer type,
                                        @Param("difficulty") Integer difficulty,
                                        @Param("limit") Integer limit);

    /**
     * 更新题目使用次数
     * 
     * @param questionId 题目ID
     * @return 更新行数
     */
    @Update("UPDATE exam_question SET use_count = use_count + 1, update_time = NOW() WHERE id = #{questionId}")
    int incrementUseCount(@Param("questionId") String questionId);

    /**
     * 更新题目正确次数
     * 
     * @param questionId 题目ID
     * @return 更新行数
     */
    @Update("UPDATE exam_question SET correct_count = correct_count + 1, update_time = NOW() WHERE id = #{questionId}")
    int incrementCorrectCount(@Param("questionId") String questionId);

    /**
     * 更新题目正确率
     * 
     * @param questionId 题目ID
     * @return 更新行数
     */
    @Update("UPDATE exam_question SET correct_rate = CASE WHEN use_count > 0 THEN ROUND(correct_count * 100.0 / use_count, 2) ELSE 0 END, update_time = NOW() WHERE id = #{questionId}")
    int updateCorrectRate(@Param("questionId") String questionId);

    /**
     * 根据标签查询题目列表
     * 
     * @param tag 标签
     * @return 题目列表
     */
    List<Question> selectByTag(@Param("tag") String tag);

    /**
     * 统计题库中各类型题目数量
     * 
     * @param bankId 题库ID
     * @return 统计结果
     */
    List<Question> countByTypeInBank(@Param("bankId") String bankId);

    /**
     * 查询高频错题
     * 
     * @param limit 限制数量
     * @return 高频错题列表
     */
    @Select("SELECT * FROM exam_question WHERE use_count > 0 AND status = 1 AND is_deleted = 0 ORDER BY (use_count - correct_count) DESC LIMIT #{limit}")
    List<Question> selectFrequentWrongQuestions(@Param("limit") Integer limit);

    /**
     * 全文搜索题目
     * 
     * @param keyword 关键词
     * @return 题目列表
     */
    List<Question> searchQuestions(@Param("keyword") String keyword);
}
